﻿//编辑对话框
var dlg = new MyAppDialog(800, 580);
var tableid = "";

//列定义
var cols = [[
        { field: "Id", title: 'Id', width: 10, hidden: true },
        { field: "Tableid", title: '表名', sortable: true, width: 100, align: 'left' },
        { field: "Fieldkind", title: '字段类别', sortable: true, width: 100, align: 'left' },
        { field: "Sort", title: '排序', sortable: true, width: 40, align: 'left' },
        { field: "Fieldid", title: '字段代码', sortable: true, width: 100, align: 'left' },
        { field: "Fieldname", title: '中文名', sortable: true, width: 100, align: 'center' },
        { field: "Fieldname_en", title: '英文名', sortable: true, width: 100, align: 'center' },
        { field: "Inputtype", title: '控件类型', sortable: true, width: 50, align: 'center' },
        { field: "Islocked", title: '停用', sortable: true, width: 40, align: 'center', formatter: function (value, rec) { return value == "1" ? "是" : "否"; } }
]];

$(document).ready(function () {
    //删除
    $("#btndel").click(function (evt) { evt.preventDefault(); del(); });
    //添加
    $("#btnadd").click(function (evt) {
        evt.preventDefault();
        dlg.setURL("新增字段", "/Manage/Fields/FieldEdit/0?t=" + tableid + "&" + Math.random());
        dlg.show();
    });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.tableid = tableid;

        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
	    if (event.keyCode == $.ui.keyCode.ENTER) {
	        event.preventDefault();
	        $("#btnquery").click();
	        return false;
	    }
    });

    //数据表
    $("#tabletree").tree({
        data: tableJson,
        onClick: function (node) {
            tableid = node.id;
            $("#btnquery").click();
        }
    });

    var node = $("#tabletree").tree("getSelected");
    if (node != null)
        tableid = node.id;

    LoadDatagrid();
});

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '字段列表',
        url: baseurl + 'Manage/Fields/FieldListJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: true,
        queryParams: {
            tableid: "",
            s: $("#searchkey").val()
        },
        sortName: 'Sort',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            dlg.setURL("编辑字段", "/Manage/Fields/FieldEdit/" + row.Id + "?" + Math.random());
            dlg.show();
        },
        //onSelect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        //onUnselect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        toolbar: "#datagrid_toolbar"
    });
    //PaginationInit();
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    if (confirm(csms_lang.message_delete_confrim)) {
        var ids = "";
        for (var i = 0; i < row.length; i++) {
            ids += row[i].Id + ",";
        }

        $.ajax({
            type: "post",
            url: baseurl + "Manage/Fields/Delete",
            data: { ids: ids },
            success: function (e) {
                if (e.Ret == "0") {
                    $("#datagrid").datagrid("reload");
                } else {
                    $.messager.alert(csms_lang.message_error, e.Msg);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                var msg = "Http status: " + xhr.status + " " + xhr.statusText
                    + "\najaxOptions: " + ajaxOptions + "\nthrownError:" + thrownError
                    + "\n" + xhr.responseText;

                //throw new Error(msg);
                alert(xhr.status + " " + xhr.statusText);
            }
        });
    }
}